//
//  FWCNetworkTools.swift
//  微博项目001
//
//  Created by 樊武超 on 16/4/11.
//  Copyright © 2016年 樊武超. All rights reserved.
//

import UIKit
import AFNetworking
//定义请求类型枚举
enum FWCMethod: String{
    case POST="POST"
    case GET="GET"
}

class FWCNetworkTools: AFHTTPSessionManager {
    
    static let shareNetworkTool: FWCNetworkTools={
    let l=FWCNetworkTools(baseURL: NSURL(string: "https://api.weibo.com/"))
     l.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return l
    }()
    
    func loadViewController(code: String,finshed: (result: AnyObject?,error: NSError?) -> ()){
    
        let paramenters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        request(.POST, urlString: authrizon, parameters: paramenters) { (result, error) -> () in
//            print(result)
            
            finshed(result: result!, error: error)
            
        }
        
    
    
    }
    
    func loadUserInfor(token: AnyObject,uid: AnyObject,finshed:(result:AnyObject?,error: NSError?) -> ()){
    let paramenters=["access_token":token,"uid":uid]
       self.request(.GET, urlString: access_token, parameters: paramenters, finshed: finshed)
    
    }
     func request(method: FWCMethod ,urlString: String,parameters: AnyObject?,finshed: (result: AnyObject?,error: NSError?) -> ()){
        //设置完成后的调用方法
        let success = { (task: NSURLSessionDataTask,result: AnyObject?) -> Void in
            
            finshed(result: result, error: nil)
        }
        //设置失败了的调用方法
        let faild = { (task: NSURLSessionDataTask?,error: NSError) -> Void in
            finshed(result: nil, error: error)
        }
      //根据不同请求类型来发送不同的请求
        if method == .GET {
        
        GET(urlString, parameters: parameters, progress: nil, success: success, failure: faild)
        
        }else{
        POST(urlString, parameters: parameters, progress: nil , success: success, failure: faild)
         }
        
    }
    //加载首页数据
    func loadHomePage(token: String,finshed: (result: AnyObject?,error: NSError?) -> ()){
        let paramenters = ["access_token": token]
        
        request(.GET, urlString: homepage, parameters: paramenters, finshed: finshed)
    
    }
    
}
